package org.lifxue.jqda.ui;

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.SwingUtilities;

import org.lifxue.jqda.data.DataUtil;
import org.lifxue.jqda.jintellitype.GlobalHotkeyResourceManagement;

/**
 * 
 * @Description:
 * @Author: zhangzuoqiang
 * @Date: Sep 26, 2011
 */
public class TrayPopupMenu {

	// private static Log logger = LogFactory.getLog(TrayPopupMenu.class);
	public static PopupMenu pm;
	public static Desktop desktop;
	public static SystemTray st;

	public void tray() {
		if (Desktop.isDesktopSupported()) {// 判断当前平台是否支持Desktop类
			desktop = Desktop.getDesktop();
		}
		if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘
			st = SystemTray.getSystemTray();
			URL url = this.getClass().getResource("butterfly16.png");
			Image image = Toolkit.getDefaultToolkit().getImage(url);
			createPopupMenu();// 创建菜单
			TrayIcon ti = new TrayIcon(image, "【ALT+空格】快速启动框；【ALT+Z】搜索界面", pm);
			try {
				st.add(ti);
			} catch (AWTException ex) {
			}
		}
	}

	private void createPopupMenu() {

		pm = new PopupMenu();
		MenuItem keyWords = new MenuItem("热键设置");
		keyWords.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						HotKeyJDialog hk = HotKeyJDialog.getInstance(true);
						hk.setVisible(true);
					}
				});
			}
		});

		MenuItem indexItem = new MenuItem("索引设置");
		indexItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						IndexerSettingJDialog is = IndexerSettingJDialog
								.getInstance(true);
						is.setVisible(true);
					}
				});
			}
		});

		MenuItem setItem = new MenuItem("快速启动设置");
		setItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						QuickStartListDialog QSList = QuickStartListDialog
								.getInstance(true);
						QSList.setVisible(true);

					}
				});
			}
		});

		MenuItem lookandfeelItem = new MenuItem("界面风格设置");
		lookandfeelItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						LookAndFeelJDialog laf = LookAndFeelJDialog
								.getInstance(true);
						laf.setVisible(true);
					}
				});
			}
		});

		MenuItem showAppFrame = new MenuItem("显示快速启动框");
		showAppFrame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				openAppframe();
			}
		});

		MenuItem showSearchFrame = new MenuItem("显示文件搜索界面");
		showSearchFrame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				openAppframe();
			}
		});

		MenuItem aboutMenu = new MenuItem("关于...");
		aboutMenu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						AboutJDialog about = AboutJDialog.getInstance(true);
						about.setVisible(true);
					}
				});
			}
		});

		MenuItem exitMenu = new MenuItem("退出程序");
		exitMenu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				GlobalHotkeyResourceManagement.releaseResources();
				DataUtil.updateAppsFile();
				DataUtil.updateConfigFile();
				System.exit(0);
			}
		});
		pm.add(showAppFrame);
		pm.add(showSearchFrame);
		pm.addSeparator();
		pm.add(keyWords);
		pm.add(setItem);
		pm.add(lookandfeelItem);
		pm.add(indexItem);
		pm.addSeparator();
		pm.add(aboutMenu);
		pm.add(exitMenu);
	}

	// 打开快速启动框
	private void openAppframe() {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {
						QuickStartJFrame qs = QuickStartJFrame.getInstance();
						qs.setVisible(true);
					}
				});
			}
		});

	}
}
